﻿using ERPWinFormWork.Material.Material.Dtos;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERPWinFormWork.Material
{
    public partial class TaskModel : Form
    {
        public string SelectedTaskName { get; private set; }
        public string SelectedTaskNumber { get; private set; }
        public string SelectedTechnologicalProcess { get; private set; }
        public string SelectedProcessName { get; private set; }

        public TaskModel()
        {
            InitializeComponent();
            _apiClient = new ApiClient(MaterialAppConfig.ApiBaseUrl);
            InitializeDataGridView();
            GetData();
            
            // 绑定按钮事件
            btnsure3.Click += Btnsure3_Click;
            btnquxiao3.Click += Btnquxiao3_Click;
        }

        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        private void Btnsure3_Click(object sender, EventArgs e)
        {
            // 查找选中的行
            for (int i = 0; i < dgvtest.Rows.Count; i++)
            {
                DataGridViewCheckBoxCell checkBoxCell = dgvtest.Rows[i].Cells[0] as DataGridViewCheckBoxCell;
                if (checkBoxCell != null && checkBoxCell.Value != null && (bool)checkBoxCell.Value)
                {
                    // 获取选中行的数据
                    SelectedTaskName = dgvtest.Rows[i].Cells[2].Value?.ToString() ?? ""; // 任务名称
                    SelectedTaskNumber = dgvtest.Rows[i].Cells[1].Value?.ToString() ?? ""; // 任务编号
                    SelectedTechnologicalProcess = dgvtest.Rows[i].Cells[4].Value?.ToString() ?? ""; // 工艺路线
                    SelectedProcessName = dgvtest.Rows[i].Cells[5].Value?.ToString() ?? ""; // 工序名称
                    
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                    return;
                }
            }
            
            MessageBox.Show("请先选择一条记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        private void Btnquxiao3_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 初始化DataGridView，添加复选框列
        /// </summary>
        private void InitializeDataGridView()
        {
            // 清空现有列
            dgvtest.Columns.Clear();
            
            // 添加复选框列
            DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
            checkBoxColumn.HeaderText = "选择";
            checkBoxColumn.Name = "CheckBoxColumn";
            checkBoxColumn.Width = 50;
            dgvtest.Columns.Add(checkBoxColumn);
            
            // 添加任务编号列
            DataGridViewTextBoxColumn taskNumberColumn = new DataGridViewTextBoxColumn();
            taskNumberColumn.HeaderText = "任务编号";
            taskNumberColumn.Name = "TaskNumberColumn";
            taskNumberColumn.DataPropertyName = "Task_Number";
            taskNumberColumn.Width = 120;
            dgvtest.Columns.Add(taskNumberColumn);
            
            // 添加任务名称列
            DataGridViewTextBoxColumn taskNameColumn = new DataGridViewTextBoxColumn();
            taskNameColumn.HeaderText = "任务名称";
            taskNameColumn.Name = "TaskNameColumn";
            taskNameColumn.DataPropertyName = "Task";
            taskNameColumn.Width = 150;
            dgvtest.Columns.Add(taskNameColumn);
            
            // 添加站点名称列
            DataGridViewTextBoxColumn siteNameColumn = new DataGridViewTextBoxColumn();
            siteNameColumn.HeaderText = "站点名称";
            siteNameColumn.Name = "SiteNameColumn";
            siteNameColumn.DataPropertyName = "Name";
            siteNameColumn.Width = 120;
            dgvtest.Columns.Add(siteNameColumn);
            
            // 添加工艺路线列
            DataGridViewTextBoxColumn technologicalProcessColumn = new DataGridViewTextBoxColumn();
            technologicalProcessColumn.HeaderText = "工艺路线";
            technologicalProcessColumn.Name = "TechnologicalProcessColumn";
            technologicalProcessColumn.DataPropertyName = "Technological_Process";
            technologicalProcessColumn.Width = 150;
            dgvtest.Columns.Add(technologicalProcessColumn);
            
            // 添加工序名称列
            DataGridViewTextBoxColumn processNameColumn = new DataGridViewTextBoxColumn();
            processNameColumn.HeaderText = "工序名称";
            processNameColumn.Name = "ProcessNameColumn";
            processNameColumn.DataPropertyName = "Process_Name";
            processNameColumn.Width = 120;
            dgvtest.Columns.Add(processNameColumn);
            
            // 添加工序编号列
            DataGridViewTextBoxColumn processNumberColumn = new DataGridViewTextBoxColumn();
            processNumberColumn.HeaderText = "工序编号";
            processNumberColumn.Name = "ProcessNumberColumn";
            processNumberColumn.DataPropertyName = "Process_Number";
            processNumberColumn.Width = 120;
            dgvtest.Columns.Add(processNumberColumn);
            
            // 设置复选框列只能单选
            dgvtest.CellClick += Dgvtest_CellClick;
        }

        /// <summary>
        /// DataGridView单元格点击事件，实现单选功能
        /// </summary>
        private void Dgvtest_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >= 0) // 点击复选框列
            {
                // 取消其他行的选中状态
                for (int i = 0; i < dgvtest.Rows.Count; i++)
                {
                    if (i != e.RowIndex)
                    {
                        dgvtest.Rows[i].Cells[0].Value = false;
                    }
                }
                
                // 设置当前行复选框状态
                bool currentValue = dgvtest.Rows[e.RowIndex].Cells[0].Value == null ? false : (bool)dgvtest.Rows[e.RowIndex].Cells[0].Value;
                dgvtest.Rows[e.RowIndex].Cells[0].Value = !currentValue;
            }
        }

        private int PageIndex { get; set; } = 1;
        private int PageSize { get; set; } = 2;
        private int TotalCount { get; set; } = 0;
        private int TotalPages { get; set; } = 0;

        public class ApiResponse<T>
        {
            public int code { get; set; }
            public string message { get; set; }
            public string jwt { get; set; }
            public T pageData { get; set; }
            public T data { get; set; }
            public int totalCount { get; set; }
            public int totalPages { get; set; }
        }

        private ApiClient _apiClient;

        private async void GetData(Dictionary<string, string> customParams = null)
        {
            var parameters = new Dictionary<string, string>
                {
                    { "PageIndex", PageIndex.ToString() },
                    { "PageSize", PageSize.ToString() },
                };

            // 合并自定义参数（查询条件）
            if (customParams != null)
            {
                foreach (var kv in customParams)
                {
                    parameters[kv.Key] = kv.Value;
                }
            }
            var response = await _apiClient.GetAsync<ApiResponse<List<Work_TaskOutput>>>(
                    endpoint: "Material/GetWorkTaskShow",
                    parameters: parameters // 传递查询参数
                );
            // 处理响应结果
            if (response != null && response.code == 200)
            {
                // 将数据绑定到 DataGridView
                dgvtest.DataSource = response.pageData;
                
                // 初始化复选框列的值
                foreach (DataGridViewRow row in dgvtest.Rows)
                {
                    row.Cells[0].Value = false;
                }
            }
        }
    }
}
